/******************************************************************************
秋夜静，独坐对残灯。
啼笑非非谁识我，坐行梦梦尽缘君，何所慰消沉。
风卷雨，雨复卷侬心。
心似欲随风雨去，茫茫大海任浮沉。
无爱亦无恨。
******************************************************************************/
#include <string.h>
//#include "cledMatrixView.h"
//#include "user_parameter.h"
#include "Curve/Curve.h"
//#include "keyboard/kb.h"
//#include "Effects/kbRGB_Int/kbRGB_Int.h"
//#include "Effects/kbRGB/kbRGB.h"
#include "Effects/kbRGB_Int/kbRGB_Int.h"
#include "Effects/kbRGB_Int/kbRGB_Int_Effect.h"
#include "Effects/kbRGB_Int/kbRGB_Int_type.h"

#define MLED_KB_MAX   104  // 定义键盘灯的数量
#define IC_Length     MLED_KB_MAX


void mled_view_kb_Init(struct kbRGB_Int_Params *const params)
{
    int index;
    hsv_t hsv = { 0, 0, 0 };
    hsv.hue = 0;
    hsv.saturation = 255;
    hsv.value = 255;

    for(index=0; index<7; index++)
    {
        hsv.hue = index*360/7;
        params->Palette[index+1] = hsv2rgb(&hsv);
    }
    params->Palette[0] = 0;
}

//const uint32_t Palette[PaletteSize] = {COLOR_RGB_RED, COLOR_RGB_GREEN, COLOR_RGB_YELLOW, COLOR_RGB_BLUE, COLOR_RGB_PURPLE, COLOR_RGB_CYAN, COLOR_RGB_WHITE};
//static enum user_param_view_mode_t view_mode;// = user_param_get_mode();
// 按下的按键坐标 :KEY_P
//int16_t key_px = 2;
//int16_t key_py = 8;
// 更新并获取按下按键的坐标
//uint16_t mled_kb_update_press(const uint16_t pix, kb_matrix_t mled_kb_value, const kb_matrix_t kb_press)
//{
//#if 0
//	int index, i;
//	struct mled_kb_pix pixel = {.pix=pix};
//	// 检测按键
//	for(index=0; index<KB_ROW; index++)
//	{
//		for(i=0; i<KB_COL; i++)
//		{
//			if((0==mled_kb_value[index][i]) && (mled_kb_value[index][i]!=kb_press[index][i]))
//			{
//				pixel.x = i;
//				pixel.y = index;
//			}
//			mled_kb_value[index][i] = kb_press[index][i];
//		}
//	}
//#else
//	struct mled_kb_pix pixel = {.pix=pix};
//	pixel.x = 10;
//	pixel.y = 2;
//#endif
//	return pixel.pix;
//}

//// 呼吸模式
////int mled_view_breathe(const uint32_t BackColor, const uint8_t Grayscale, uint8_t Speed)
//int mled_view_breathe(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
//{
//#if 1
//	uint32_t color;
//	//Draw.brush_color(Draw.backdrop);
//	mled_fillScreen(background, 0);   // 设置背景色
//	color = angle2rgb((mled_view.angle>>ANGLE_HSV_PRE));//color = angle_rgb(rgb_angle);
//	uint8_t Speed = speed<< 1;
//	mled_view.angle = angle_hsv_up(mled_view.angle, Speed);
//	if(mled_view.angle<Speed) mled_view.angle += 1;
//	mled_fillScreen(color, 0xFF);   // 设置背景色
//#else
//	uint32_t color;
//	mled_fillScreen(BackColor, Grayscale);   // 设置背景色
//	Speed -= 3;
//	mled_view.angle = angle_hsv_up(mled_view.angle, Speed);
//	if(mled_view.angle<Speed) mled_view.angle += 1;
//	color = angle_hsv_rgb(mled_view.angle);
//	mled_fillScreen(color, Grayscale);   // 设置背景色
//#endif
//	return 0;
//}
// 涟漪模式
#if 0
int mled_view_ripple(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
{
	//struct mled_view_t* const mled = &params->mled_view;
	const uint8_t Speed = speed+3;
	int i;
    int16_t cIndex;
	uint32_t color;
	uint16_t cnt=params->progress>>ANGLE_HSV_PRE;//mled_view.angle;
	struct mled_kb_pix pixel;
	pixel.x = 10;
	pixel.y = 2;
	// 检测用户按键操作
	//mled_view.kb.pix = mled_kb_update_press(mled_view.kb.pix, mled->keys, key_board_value);
	//qDebug("[%s--%d] kx:%d ky:%2d\r\n", __func__, __LINE__, kx, ky);
    //params->progress = angle_hsv_up(params->progress, Speed);   //  更新混色相位, RGB 三色以120°相位差的正弦曲线混合
	params->progress = params->progress+Speed; params->progress = params->progress%(360<<ANGLE_HSV_PRE);
	if(params->progress<Speed) params->progress += 1;
	//qDebug("[%s--%d] progress:%4d speed:%2d\r\n", __func__, __LINE__, params->progress, speed);
	cIndex=cnt;
	//qDebug("[%s--%d] Gray:%3d GrayMode:%d index_move:%3d bar_len:%2d cIndex:%2d\r\n", __func__, __LINE__, Gray, GrayMode, index_move, bar_len, cIndex);
	for(i=0; i<32; i++)//for(i=pixSize-1; i>=0; i--)//for(i=0; i<pixSize; i++)
	{
		// 取色 
		color = angle2rgb(cIndex);
		// 更新相位
		if(reverse) 
		{
			cIndex = cIndex + (2*9);
			if(cIndex>=360) cIndex = cIndex-360;
		}
		else
		{
			cIndex = cIndex - (2*9);
			if(cIndex<0) cIndex = cIndex+360;
		}
		// 绘制方形(菱形)
		mled_draw_orthogon(pixel.x, pixel.y, i, color/*, Grayscale*/); // Draw.drawOrthogon(kx, ky, i, color);
	}
	return 0;
}
#endif
/*******************************************************************************
 *          3       |     4
 * -----------------|---------------------
 *          2       |     1
*******************************************************************************/
static inline uint32_t Crater_GetColor(const uint16_t x, const uint16_t y, const int16_t cx, const int16_t cy, const int32_t _progress, const int reverse)
{
    //struct kbRGB_Pixel piont;
    uint32_t hue;
    int16_t angle = kbRGB_XY2Angle(x-cx, y-cy);
    angle = angle % (360<<KBRGB_XY2ANGLE_PRE);
    if(angle<=(90<<KBRGB_XY2ANGLE_PRE)) angle = angle;
    else if(angle<=(180<<KBRGB_XY2ANGLE_PRE)) angle = (180<<KBRGB_XY2ANGLE_PRE)-angle;
    else if(angle<=(270<<KBRGB_XY2ANGLE_PRE)) angle = angle-(180<<KBRGB_XY2ANGLE_PRE);
    else if(angle<=(360<<KBRGB_XY2ANGLE_PRE)) angle = (360<<KBRGB_XY2ANGLE_PRE)-angle;
    angle = angle>>(KBRGB_XY2ANGLE_PRE-1);
    if(0==reverse) angle = 180-angle;
    hue = ((int)(_progress/kbRGB_PRE_MAX + 360 + (y - cy)*5 + angle) % 360);
    hsv_t hsv = { 0, 0, 0 };
    hsv.hue = (int)hue;
    hsv.saturation = 255;
    hsv.value = 255;

    return (hsv2rgb(&hsv));
}
// 火山口
void mled_view_Crater(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
{
    const int16_t cx = (cols - 1)>>1;
    //const int16_t cy = -1;//(rows - 1)>>1;
    const int16_t cy = rows - ((rows - 1)>>2);

    for (uint16_t col_id = 0; col_id < cols; col_id++)
    {
         for (uint16_t row_id = 0; row_id < rows; row_id++)
        {
            uint32_t color = Crater_GetColor(col_id, row_id, cx, cy, params->progress, !reverse);
            kbRGB_pset(col_id, row_id, color);
        }
    }
    params->progress += ((params->Speed+speed*30+60)<<kbRGB_PRE_BIT) / params->FPS;
}
/*******************************************************************************
 *          3       |     4
 * -----------------|---------------------
 *          2       |     1
*******************************************************************************/
// 索伦之眼
static inline uint32_t EyeofSaurone_GetColor(const uint16_t x, const uint16_t y, const int16_t cx, const int16_t cy, const int32_t _progress, const int reverse)
{
    uint32_t radius = kb_sqrt((x - cx)*(x - cx)*100 + (y - cy)*(y - cy)*100/2.0f);
    //struct kbRGB_Pixel piont;
    uint32_t hue;
    //piont.pixel = kbRGB_Angle_IntGet((x - cx), (y - cy));
    int16_t angle = kbRGB_XY2Angle((x - cx), (y - cy));
    if(angle<=(90<<KBRGB_XY2ANGLE_PRE)) angle = angle;
    else if(angle<=(180<<KBRGB_XY2ANGLE_PRE)) angle = (180<<KBRGB_XY2ANGLE_PRE)-angle;
    else if(angle<=(270<<KBRGB_XY2ANGLE_PRE)) angle = angle-(180<<KBRGB_XY2ANGLE_PRE);
    else if(angle<=(360<<KBRGB_XY2ANGLE_PRE)) angle = (360<<KBRGB_XY2ANGLE_PRE)-angle;
    angle = angle>>(KBRGB_XY2ANGLE_PRE-0);
    //if(0==reverse) piont.angle = (90<<(KBRGB_ANGLE_INT_PRE-0))-piont.angle;
    int16_t diff = (x - cx); if(diff<0) diff = -diff;
    angle = angle + (cx-diff)*7;
    hue = ((int)(1*_progress/kbRGB_PRE_MAX + 360 + 2.5f*angle+radius/10) % 360);
    hsv_t hsv = { 0, 0, 0 };
    hsv.hue = (int)hue;
    hsv.saturation = 255;
    hsv.value = 255;

    return (hsv2rgb(&hsv));
}
void mled_view_EyeofSauron(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
{
    const int16_t cx = (cols - 1)>>1;
    //const int16_t cy = -1;//(rows - 1)>>1;
    //const int16_t cy = rows - ((rows - 1)>>2);
    const int16_t cy = ((rows - 1)>>1);

    for (uint16_t col_id = 0; col_id < cols; col_id++)
    {
        for (uint16_t row_id = 0; row_id < rows; row_id++)
        {
            uint32_t color = EyeofSaurone_GetColor(col_id, row_id, cx, cy, params->progress, !reverse);
            //kbRGB_pset(col_id, row_id, color);
            kbRGB_pset(col_id, (row_id+rows/2)%rows, color);
            //kbRGB_pset((col_id+cols/2)%cols, row_id, color);
        }
    }
    params->progress += ((params->Speed+speed*30+60)<<kbRGB_PRE_BIT) / params->FPS;
}
/*******************************************************************************
 *          3       |     4
 * -----------------|---------------------
 *          2       |     1
*******************************************************************************/
static inline uint32_t ripple_GetColor(const uint16_t x, const uint16_t y, const int16_t cx, const int16_t cy, const int32_t _progress, const int reverse)
{
    //float direction_mult = -1.0f;//direction == 0 ? 1.f : -1.f;
    //double angle;
    //struct kbRGB_Pixel piont;
    uint32_t hue;// = (float)(progress + (int)(180 + direction_mult * (reverse ? atan2(y - cy, x - cx) : atan2(x - cx, y - cy)) * (180.0 / 3.14159)) % 360);
    //piont.pixel = kbRGB_Angle_IntGet((x - cx), (y - cy));
    int16_t angle = kbRGB_XY2Angle((x - cx), (y - cy));
    //piont.angle = piont.angle%(180<<KBRGB_ANGLE_INT_PRE);
    if(angle<=(90<<KBRGB_XY2ANGLE_PRE)) angle = angle;
    else if(angle<=(180<<KBRGB_XY2ANGLE_PRE)) angle = (180<<KBRGB_XY2ANGLE_PRE)-angle;
    else if(angle<=(270<<KBRGB_XY2ANGLE_PRE)) angle = angle-(180<<KBRGB_XY2ANGLE_PRE);
    else if(angle<=(360<<KBRGB_XY2ANGLE_PRE)) angle = (360<<KBRGB_XY2ANGLE_PRE)-angle;
    angle = angle>>(KBRGB_XY2ANGLE_PRE-0);
    if(0==reverse) angle = (90<<(KBRGB_XY2ANGLE_PRE-0))-angle;
    //angle = piont.angle/4.0f;
    //hue = (float)(progress + (int)(180 + direction_mult * (reverse ? atan2(y - cy, x - cx) : atan2(x - cx, y - cy)) * (180.0 / 3.14159)) % 360);
    //hue = (_progress/kbRGB_PRE_MAX + (int)(360 + /*direction_mult **/(reverse ? piont.angle>>2 : 90-(piont.angle>>2))) % 360);
    int16_t diff = (x - cx); if(diff<0) diff = -diff;
    //if(piont.angle<(90<<(KBRGB_ANGLE_INT_PRE-0))) piont.angle = piont.angle + (cx-diff)*2;
    angle = angle + (cx-diff)*4;
//    diff = (y - cy); if(diff<0) diff = -diff;
//    if((90<<(KBRGB_ANGLE_INT_PRE-0))==piont.angle) piont.angle = piont.angle + (cy-diff)*2;
    hue = ((int)(1*_progress/kbRGB_PRE_MAX + 360 + (x - cx)*0 + (y - cy)*0 + 3*angle) % 360);
//    int16_t dy = (y - cy);
//    if(dy<0) dy = -dy;
//    if(0==(x - cx)) hue = ((int)(0*_progress/kbRGB_PRE_MAX + 360 + (x - cx)*0 + dy*3 + 3*piont.angle) % 360);
    //hue = (float)(progress + (int)(180 + (atan2(x - cx, y - cy)) * (180.0 / 3.14159)) % 360);
    //hue = (float)(progress + (int)(360 - angle) % 360);
    hsv_t hsv = { 0, 0, 0 };
    hsv.hue = (int)hue;
    hsv.saturation = 255;
    hsv.value = 255;

    return (hsv2rgb(&hsv));
}
void mled_view_ripple(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
{
    const int16_t cx = (cols - 1)>>1;
    //const int16_t cy = -1;//(rows - 1)>>1;
    //const int16_t cy = rows - ((rows - 1)>>2);
    const int16_t cy = ((rows - 1)>>1);

    for (uint16_t col_id = 0; col_id < cols; col_id++)
    {
         for (uint16_t row_id = 0; row_id < rows; row_id++)
        {
            uint32_t color = ripple_GetColor(col_id, row_id, cx, cy, params->progress, !reverse);
            kbRGB_pset(col_id, row_id, color);
            //kbRGB_pset(col_id, (row_id+rows/2)%rows, color);
            //kbRGB_pset((col_id+cols/2)%cols, row_id, color);
        }
    }
    params->progress += (params->Speed<<kbRGB_PRE_BIT) / params->FPS;
}
void mled_view_cnt(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
{
    uint16_t progress = params->Progress;
    params->hsv.hue++;
    if(params->hsv.hue>=360) params->hsv.hue = 0;
    params->hsv.saturation = 255;
    params->hsv.value = 255;
    uint32_t color = hsv2rgb(&params->hsv);
    for (uint16_t col_id = 0; col_id < cols; col_id++)
    {
         for (uint16_t row_id = 0; row_id < rows; row_id++)
        {
            if((row_id*cols+col_id)<=progress) kbRGB_pset(col_id, row_id, color);
        }
    }
    params->Progress += (params->Speed+speed*5) / (double)params->FPS;
    if(params->Progress>=(cols*rows))
    {
        params->Progress = 0;
    }
}
#if 0
// 扩散模式
#define LED_Length       33
// 混色
static inline uint32_t mled_rgb_mixture(const uint32_t color, const uint32_t add_color, const uint8_t mixture1)
{
	struct BRGB rgb1, rgb2;
	rgb1.color = color;
	rgb2.color = add_color;
	rgb1.red   = (rgb2.red*(255-mixture1)+rgb1.red*mixture1+255)>>8;
	rgb1.green = (rgb2.green*(255-mixture1)+rgb1.green*mixture1+255)>>8;
	rgb1.blue  = (rgb2.blue*(255-mixture1)+rgb1.blue*mixture1+255)>>8;
	rgb1.bright = 0xFF;
	return rgb1.color;
}
// 扩散模式
int mled_view_diffusion(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
{
	struct mled_view_diffusion_t* const mled = &params->mled_view_Diffusion;
	uint32_t color1, color2;
	int row_id;
	int index,i;
	uint8_t Speed = speed+3;
	uint16_t radii;

	for(index=0; index<KB_ROW; index++)
	{
		for(i=0; i<KB_COL; i++)
		{
			if((0==mled->keys[index][i]) && (mled->keys[index][i]!=key_board_value[index][i]))
			{
				for(int k=0; k<7; k++)
				{
					if((-1==mled->pixel[k].x) || (-1==mled->pixel[k].y)) 
					{ 
						mled->pixel[k].x=i; 
						mled->pixel[k].y=index;
						mled->radii[k]=0; 
						mled->cnt[k]=mled->color_cnt+1; 
						break;
					}
				}
				mled->color_cnt++; if(mled->color_cnt>=PaletteSize) mled->color_cnt = 0;
			}
			mled->keys[index][i] = key_board_value[index][i];
		}
	}
	mled_fillScreen(params->background, 0);       // 设置背景色
	//Draw.brush_color(0x00000000);
	//for(index=0; index<5; index++) qDebug("[%s--%d] move_pos[%d]:%d \r\n", __func__, __LINE__, index, move_pos[index]);
	Speed -= 1;
	for(index=0; index<7; index++) 
	{
		if(mled->pixel[index].x>=0)
		{
			//radii = mled_view.radii[index]>>2;
			radii = mled->radii[index]>>(3+ANGLE_HSV_PRE);
			color1 = params->Palette[mled->cnt[index]];//
			color2 = mled_get_pixel(mled->pixel[index].x+radii+1, 0);
			color2 = mled_rgb_mixture(color1, color2, 100);
			//mled_draw_FastVLine(mled_view.pixel[index].x+radii+1, 0, LEDMatrixHeight, color2/*, Grayscale*/);
			for(row_id=0; row_id<rows; row_id++) mled_draw_pixel(mled->pixel[index].x+radii+1, row_id, color2);
			
			color2 = mled_get_pixel(mled->pixel[index].x-radii-1, 0);
			color2 = mled_rgb_mixture(color1, color2, 100);
			//mled_draw_FastVLine(mled_view.pixel[index].x-radii-1, 0, LEDMatrixHeight, color2/*, Grayscale*/);
			for(row_id=0; row_id<rows; row_id++) mled_draw_pixel(mled->pixel[index].x-radii-1, row_id, color2);
			mled->radii[index]+=Speed;
			if(((mled->pixel[index].x-radii-1)<0) && ((mled->pixel[index].x+radii+1)>LEDMatrixWidth))
			{
				mled->pixel[index].x=-1; mled->pixel[index].y=-1;
			}
		}
	}
	return 0;
}
// 波浪模式
int mled_view_waveH(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
{
	int col_id, row_id;
	uint32_t color;
	const uint8_t Speed = speed+3;
	int16_t cIndex=params->progress>>ANGLE_HSV_PRE;
	mled_fillScreen(params->background, 0);       // 设置背景色
    //qDebug("[%s--%d] default ", __func__, __LINE__);
//    int16_t cIndex;//, _cIndex;
	//enum user_param_view_move_t view_move = user_param_get_move();
	//qDebug("[%s--%d] move:0x%02X \r\n", __func__, __LINE__, view_move);
	params->progress = params->progress+Speed; params->progress = params->progress%(360<<ANGLE_HSV_PRE);
	if(params->progress<Speed) params->progress += 1;
    //qDebug("\r\n[%s--%d] angle:%3d view_move:%3d", __func__, __LINE__, rgb_angle, view_move);
	// 从左往右或从右往左移动
	for(col_id=0; col_id<cols; col_id++)
	{
		// 取色 
		color = angle2rgb(cIndex);
		// 更新相位
		if(reverse) 
		{
			cIndex = cIndex + (2*9);
			if(cIndex>=360) cIndex = cIndex-360;
		}
		else
		{
			cIndex = cIndex - (2*9);
			if(cIndex<0) cIndex = cIndex+360;
		}
		for(row_id=0; row_id<rows; row_id++) mled_draw_pixel(col_id, row_id, color);
	}
	return 0;
}
int mled_view_waveV(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
{
	int col_id, row_id;
	uint32_t color;
	const uint8_t Speed = speed;
	int16_t cIndex=params->progress>>ANGLE_HSV_PRE;
	mled_fillScreen(params->background, 0);       // 设置背景色
	params->progress = params->progress+Speed; params->progress = params->progress%(360<<ANGLE_HSV_PRE);
	if(params->progress<Speed) params->progress += 1;
    //qDebug("\r\n[%s--%d] angle:%3d view_move:%3d", __func__, __LINE__, rgb_angle, view_move);
	// 从上往下或从下往上移动
	for(row_id=0; row_id<rows; row_id++)
	{
		// 取色 
		color = angle2rgb(cIndex);
		// 更新相位
		if(reverse) 
		{
			cIndex = cIndex + (2*9);
			if(cIndex>=360) cIndex = cIndex-360;
		}
		else
		{
			cIndex = cIndex - (2*9);
			if(cIndex<0) cIndex = cIndex+360;
		}
		for(col_id=0; col_id<cols; col_id++) mled_draw_pixel(col_id, row_id, color);
	}
	return 0;
}
// 恒亮模式
int mled_view_kb_bright(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
{
	const uint8_t Grayscale = 0xFF;
	const uint8_t index = up_get_color_normally_on();
	//qDebug("[%s--%d] index:%d PaletteSize:%d\r\n", __func__, __LINE__, index, PaletteSize);
	if(index<PaletteSize)
	{
		mled_fillScreen(params->Palette[index+1], Grayscale);       // 设置背景色
	}
	else
	{
		uint32_t color;
		mled_fillScreen(params->background, Grayscale);       // 设置背景色
		uint16_t cnt=0;
		for (int i = 0; i < KB_COL; i++)
		{
			color = angle2rgb(cnt%360);
			//cnt = angle_cnt_up(cnt, 18*2);
			cnt = cnt + 18*2;
			for(int k=0; k<KB_ROW; k++)
			{
				mled_draw_pixel(i, k, color);
			}
		}
	}
	return 0;
}
// Luma Cycle, 踏雪无痕模式
int mled_view_kb_LumaCycle(const uint16_t cols, const uint16_t rows, const uint8_t speed, const int reverse, struct kbRGB_Int_Params *const params)
{
	struct mled_view_t* const mled = &params->mled_view;
	int col_id, row_id;
	uint8_t cIndex;
	uint8_t cnt;
	hsv_t hsv;
	uint32_t color;
	hsv.saturation = 255;
	hsv.value = 255;
	// 更新颜色
	cIndex = up_get_color_normally_on();
	hsv.hue = 360*cIndex/7;
	color = hsv2rgb(&hsv);
	// 检测按键
	for(row_id=0; row_id<rows; row_id++)
	{
		for(col_id=0; col_id<cols; col_id++)
		{
			if((0==mled->keys[row_id][col_id]) && (mled->keys[row_id][col_id]!=key_board_value[row_id][col_id]))
			{
				cnt = mled->rgb[row_id][col_id]+1;
				cnt = cnt&0x07;//if(cnt>=8) cnt=0;
				mled->rgb[row_id][col_id] = cnt;
				cnt = cIndex+mled->rgb[row_id][col_id];
				cnt = cnt&0x07;
				if(cnt>0) mled_draw_pixel(col_id, row_id, params->Palette[cnt]);
				else mled_draw_pixel(col_id, row_id, color);
			}
			mled->keys[row_id][col_id] = key_board_value[row_id][col_id];
		}
	}
	for(row_id=0; row_id<rows; row_id++)
	{
		for(col_id=0; col_id<cols; col_id++)
		{
			color = mled_get_pixel(col_id, row_id);
			cnt = cIndex+mled->rgb[row_id][col_id];
			cnt = cnt&0x07;
			//if((0==index) && (0==i)) qDebug("[%s--%d] cIndex:%d %d color:0x%08X brightness:%d %3d\r\n", __func__, __LINE__, cIndex, (cIndex<PaletteSize), angle_rgb(brightness*4), brightness, (sin_rgb_blend[brightness]-127)<<1);
			rgb2hsv(color, &hsv);
			if(hsv.value>0) hsv.value--;
			if(0==cnt)
			{
				hsv.hue +=2;
				hsv.hue = hsv.hue%360;
				hsv.saturation = hsv.value;
			}
			color = hsv2rgb(&hsv);
			mled_draw_pixel(col_id, row_id, color);
		}
	}
	return 0;
}

#if 0
int mled_view_kb_hid_menu(const uint32_t BackColor, const uint8_t Grayscale, uint8_t Speed)
{
	int index, i;
	uint8_t cIndex;
	// 刷新每个按键的颜色索引
	for(index=0; index<KB_ROW; index++)
	{
		for(i=0; i<KB_COL; i++)
		{
			if((0==_key_board_value[index][i]) && (_key_board_value[index][i]!=key_board_value[index][i]))
			{
				cIndex = key_board_rgb[index][i]+1;
				if(cIndex>=8) cIndex=0;
				key_board_rgb[index][i] = cIndex;
			}
			_key_board_value[index][i] = key_board_value[index][i];
		}
	}
	// 刷新每个按键的颜色
	mled_view_kb_flush(Grayscale, key_board_rgb);//model9_user_design();
	return 0;
}
#endif
#endif

